﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
using System.IO;

namespace PhysicsSimulator
{
    public class SpringData
    {
        private const string filename = "springdata.xml";
        public string Name;
        public float SpringsK;
        public float SpringsEnviromentRatio;

        public SpringData()
        {
        }
        public SpringData(string name, float k, float luccan)
        {
            Name = name;
            SpringsK = k;
            SpringsEnviromentRatio = luccan;
        }

        public static bool SaveData(List<SpringData> list)
        {
            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream stream = storage.CreateFile(filename);
            XmlSerializer xml = new XmlSerializer(typeof(List<SpringData>));
            xml.Serialize(stream, list);
            stream.Close();
            stream.Dispose();
            return true;
        }

        public static List<SpringData> Load()
        {
            List<SpringData> data = new List<SpringData>();
            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (storage.FileExists(filename))
            {
                FileStream stream = storage.OpenFile(filename, FileMode.Open);
                XmlSerializer xml = new XmlSerializer(typeof(List<SpringData>));

                data = xml.Deserialize(stream) as List<SpringData>;

                stream.Close();
                stream.Dispose();
            }
            else
            {
                return new List<SpringData>();
            }
            return data;
        }
    }
}
